<?php

class Usertasklinks extends Zend_Db_Table
{
    protected $_name = 'usertasklinks';
    
    public function checkExist($task_id,$user_id)
    {
        $select = $this->getAdapter();
        $query = 'SELECT count(*) as all_records 
                  FROM usertasklinks 
                  WHERE task_id="'.$task_id.'"
                  AND user_id="'.$user_id.'"';
        //echo $query.'<br>'; exit();
        $prepare = $select->query($query);
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        return $result[0]['all_records'];
    }
    
    public function ClearLinks($task_id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('DELETE FROM usertasklinks 
                                   WHERE task_id="'.(int)$task_id.'"');
    }
    
    public function LinkUser($task_id, $user_id)
    {
        $data = array('task_id' => $task_id,
                      'user_id' => $user_id);
        $this->insert($data);
    }
    
    public function UnlinkUser($task_id, $user_id)
    {
        $select = $this->getAdapter();
        $prepare = $select->query('DELETE FROM usertasklinks 
                                   WHERE task_id="'.(int)$task_id.'"
                                   AND user_id="'.$user_id.'"');
    }
    
    public function getMyTasksForTheProject($project_id,$user_id)
    {
        $select = $this->getAdapter();
        $query = 'SELECT usertasklinks.task_id
                  FROM usertasklinks
                  INNER JOIN task on task.id=usertasklinks.task_id
                  AND task.project_id='.$project_id.' 
                  WHERE user_id="'.$user_id.'"';
        $prepare = $select->query($query);
        $result = $prepare->fetchAll(Zend_Db::FETCH_ASSOC);
        $tasks = array();
        foreach($result as $row)
        {
            $tasks[] = $row["task_id"];
        }
        return $tasks;
    }
}